var limit = 10
var pn = 1;
var selectFood = [];
var isclick = [];
$(function() {
	queryf(1, 10);

})

function queryf(pn, limit) {

	$.ajax({
		type: "get",
		url: "foodManage",
		async: true,
		data: {
			method: "showAllFoods",
			pn: pn,
			limit: limit
		},
		dataType: "json",
		success: function(data) {

			console.log(data);
			//开始遍历
			var strs_data = '';
			var pm = data.pm; //分页对象数据
			$(data.foods).each(function(indx, food) {
				isclick[indx] = false;
				var path = food.food_pic;
				path = path.replace(/\\/g, "/");

				strs_data += '<div class="col-sm-6 col-lg-3">';
				strs_data += '<div class="card">';
				strs_data += '<div class="card-header bg-primary">';
				strs_data += '<h4>' + food.food_name + '</h4>';
				strs_data += ' <ul class="card-actions">';
				strs_data += ' <li>';
				strs_data += '<button type="button"  title="查看菜品信息" onclick="allMsg(\'' + food.food_name + '\',\'' + path + '\',' + food.food_price + ',' + food.stock + ',\'' + food.food_introduce + '\',' + food.food_grade + ')"><i class="mdi mdi-eye"></i></button>';
				strs_data += '<button type="button"  title="增加到每日特惠" onclick="addBenefit(\'' + food.food_name + '\',' + indx + ')"><i class="mdi mdi-plus"></i></button>';
				strs_data += '</li>';
				strs_data += ' </ul>';
				strs_data += '</div>';
				strs_data += '<div class="card-body" >';
				strs_data += '<img src="foodManage?method=showImage&path=' + path + '" width="185px" height="105px"  style="padding-left: 25px !important;"/>';
				strs_data += '</div>';
				strs_data += '</div>';
				strs_data += '</div>';

			});

			//将整个html代码加载到页面中
			$("#benefits").html(strs_data);

			//加载图片
			//			<div class="item active"><img src="../images/img-slide-2.jpg" alt="图片一"></div>
			//		    <div class="item"><img src="../images/img-slide-3.jpg" alt="图片二"></div>
			//		    <div class="item"><img src="../images/img-slide-4.jpg" alt="图片三"></div>

			//加载分页
			var strs_pages=''
			if(pm.pn <= 1) {
				strs_pages += ' <li class="disabled">';
				 strs_pages += ' <a href="#!">';
				 strs_pages += ' <span><i class="mdi mdi-chevron-left"></i></span>';
				strs_pages+= ' </a>';
				strs_pages += ' </li>';
			} else {
				strs_pages += '<li><a href = "javascript:queryf(' + (pm.pn - 1) + ',' + limit + ')"><span>«</span></a></li>';

			strs_pages+= ' <li >';
				strs_pages += ' <a href="javascript:queryf(' + (pm.pn - 1) + ',' + limit + ')">';
				strs_pages += ' <span><i class="mdi mdi-chevron-left"></i></span>';
				strs_pages += ' </a>';
				strs_pages += ' </li>';
			}

			for(var i = 1; i <= pm.pages; i++) {
				if(pm.pn == i) {
					//代表这一页正好是这个数字
					strs_pages += '<li class="active"><span>' + i + '</span></li>';
				} else {
					strs_pages += '<li><a href="javascript:queryf(' + i + ',' + limit + ')">' + i + '</a></li>';
				}
			}

			if(pm.pn >= pm.pages) {

				strs_pages += ' <li class="disabled">';
				strs_pages += ' <a >';
				strs_pages += ' <span><i class="mdi mdi-chevron-right"></i></span>';
				strs_pages += ' </a>';
				strs_pages += ' </li> ';

			} else {

				strs_pages += ' <li >';
				strs_pages += ' <a href="javascript:queryf(' + (pm.pn + 1) + ',' + limit + ')">';
				strs_pages += ' <span><i class="mdi mdi-chevron-right"></i></span>';
				strs_pages += ' </a>';
				strs_pages += ' </li> ';

			}

			//上一页

			//中间部分

			//strs_pages += '<li class="disabled"><span>...</span></li>';
			//strs_pages += '<li><a href="#!">14452</a></li>';
			//strs_pages += '<li><a href="#!">14453</a></li>';
			//最后一页

			//将这个分页的html字符串写到页面中
			$(".pagination").html(strs_pages);
		}

	});
}

/**
 * 展示当前菜品的所有信息
 * @param {Object} name
 * @param {Object} path
 * @param {Object} price
 * @param {Object} stock
 * @param {Object} produce
 * @param {Object} grade
 */
function allMsg(name, path, price, stock, produce, grade) {
	var strs_data = '';
	strs_data += ' <img src="foodManage?method=showImage&path=' + path + '" width="724px" height="465px"/>';
	strs_data += '  <div style="padding-top: 20px;">';

	strs_data += '  <table class="table">';
	strs_data += '    <thead>';
	strs_data += '  <tr>';
	strs_data += ' <th>名字</th>';
	strs_data += '  <th>价格</th>';
	strs_data += '  <th>评分</th>';
	strs_data += ' <th>库存</th>';
	strs_data += ' </tr>';
	strs_data += ' </thead>';
	strs_data += '  <tbody>';
	strs_data += '  <tr class="success">';
	strs_data += ' <td scope="row">' + name + '</td>';
	strs_data += ' <td>' + price + '</td>';
	if(grade == undefined || grade == null) {
		strs_data += '  <td>还未评分</td>';
	} else {
		strs_data += ' <td>' + grade + '</td>';
	}

	strs_data += ' <td>' + stock + '</td>';
	strs_data += '  </tr>';
	strs_data += ' </tbody>';
	strs_data += '  </table>';
	strs_data += ' <div class="alert alert-success" role="alert">介绍：' + produce + '</div>';
	strs_data += '  </div>';
	$("#show").html(strs_data);

	$("#button").click()
}
/**
 * 选中当前菜品并加入标签栏
 * @param {Object} name
 * @param {Object} i
 */
function addBenefit(name, i) {

	if(isclick[i] == false) {
		strs_data = '<span class="label label-success" >' + name + '</span> ';
		selectFood.push(name);
		$("#addBe").append(strs_data);
		isclick[i] = true;

	} else {
		console.log(selectFood)
		lightyear.loading('show');
		setTimeout(function() {
			lightyear.loading('hide');
			lightyear.notify('不可重复添加', 'warning', 1000, 'mdi mdi-close-circle');
		}, 1000);

	}

}
/**
 * 修改标签栏的 已选菜品
 */
function changeSfood() {
	var strs='';
	for(var i = 0; i < selectFood.length; i++) {
		strs += '<div class="alert alert-success" role="alert">' + selectFood[i] + '<a style="float: right;" onclick="removeSelectFood(' + i + ',this)">×</a> </div>';
		
	}
	console.log(strs)
$("#changeSfood").html(strs);
	$("#button2").click();
}
Array.prototype.removes = function(dx) {
	if(isNaN(dx) || dx > this.length) {
		return false;
	}
	for(var i = 0, n = 0; i < this.length; i++) {
		if(this[i] != this[dx]) {
			this[n++] = this[i]
		}
	}
	this.length -= 1
}
/**
 * 
 * @param {Object} i
 * @param {Object} obj
 */
function removeSelectFood(i, obj) {
	var k=i;
	obj.parentNode.remove()
	
	console.log(selectFood)
	isclick[i]=false
	var lable =[];
	lable =document.getElementsByClassName("label")

	console.log(selectFood[i])
	
	for (var j=0;j<lable.length;j++) {
		console.log(selectFood[i]==lable[j].textContent.trim().toString())
		if(selectFood[i]==lable[j].textContent.trim().toString())
		
		{
			
			lable[j].remove()
		}
	}
console.log(selectFood)
	selectFood.removes(i);
}
function publish(){
	var lable =[];
	var str =''
	var options = $("#se option:selected");　　　　 

	var discount = options.val();　　　　　　　　　　　　　　 

		
	lable =document.getElementsByClassName("label")
	if(selectFood==[]||selectFood==null||lable==[]||lable==null||selectFood[0]==undefined||lable[0]==undefined){
				lightyear.loading('show');
				setTimeout(function() {
				lightyear.loading('hide');
				lightyear.notify('还未添加菜品，请添加！', 'warning', 3000, 'mdi mdi-close-circle');
				}, 1000);
		
	}
	else if(discount==10){
				lightyear.loading('show');
				setTimeout(function() {
				lightyear.loading('hide');
				lightyear.notify('请选择折扣!', 'warning', 3000, 'mdi mdi-close-circle');
				}, 1000);
	}
	else{
		
		for (var i=0; i<selectFood.length;i++) {
			str += selectFood[i]+','
		}
		str=str.substring(0,str.length-1);
	$.ajax({
		type: "get",
		url: "foodManage",
		async: true,
		data: {
			method: "publishBenefit",
			benefit_foods:str,
			discount:discount
		},
		dataType: "json",
		success: function(data) {
			if(data.code==-1){
				lightyear.loading('show');
				setTimeout(function() {
				lightyear.loading('hide');
				lightyear.notify('上传失败', 'error', 3000, 'mdi mdi-close-circle');
				}, 1000);
			}
			else if(data.code==1){
				lightyear.loading('show');
				setTimeout(function() {
				lightyear.loading('hide');
				lightyear.notify('上传成功', 'success', 1000, 'mdi mdi-checkbox-marked');
				}, 1000);
			}
		}
	});
	
	}
}
